package com.lzy.system.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.cacheAnno.RedisCache;
import com.lzy.cacheAnno.RedisCacheDel;
import com.lzy.conf.RedisUtil;
import com.lzy.exception.BusException;
import com.lzy.system.organization.entity.SysRes;
import com.lzy.system.organization.entity.SysResRole;
import com.lzy.system.organization.entity.SysRole;
import com.lzy.system.organization.mapper.SysResRoleMapper;
import com.lzy.system.organization.service.ISysResRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lcc
 * @since 2022-08-17
 */
@Service
public class SysResRoleServiceImpl extends ServiceImpl<SysResRoleMapper, SysResRole> implements ISysResRoleService {

    SysResRoleMapper sysResRoleMapper;

    @Autowired
    public SysResRoleServiceImpl(SysResRoleMapper sysResRoleMapper, RedisUtil redisUtil) {
        this.sysResRoleMapper = sysResRoleMapper;
    }

    @Override
    @RedisCache(value = {"uid"}, key = "user:res_list", extKey = "#uid")
    public List<SysRes> queryByUid(String uid) {
        List<SysRes> sysRes = sysResRoleMapper.queryByUid(uid);
        return sysRes;
    }

    @Override
    @RedisCache(value = {"uid", "url"}, key = "user:res_list", extKey = "#uid+'_'+#url")
    public List<SysRes> queryByUidAndUrl(String uid, String url) throws BusException {
        List<SysRes> sysRes = sysResRoleMapper.queryByUidAndUrl(uid, url);
        return sysRes;
    }

    @Override
    public List<SysResRole> queryByResId(Long resId) throws BusException {
        QueryWrapper<SysResRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("RESID", resId);
        return list(queryWrapper);
    }

    @Override
    public List<SysResRole> queryByRoleId(Long roleId) throws BusException {
        QueryWrapper<SysResRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleid", roleId);
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @RedisCacheDel(key = "user:res_list*")
    public void savePowerByResId(Long resId, List<SysRole> sysRoles) throws BusException {
        QueryWrapper<SysResRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("resid", resId);
        remove(queryWrapper);//先删除所有的授权信息
        List<SysResRole> list = new ArrayList<>();
        for (SysRole sysRole : sysRoles) {
            list.add(new SysResRole() {{
                setResid(resId);
                setRoleid(sysRole.getId());
            }});
        }
        saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @RedisCacheDel(key = "user:res_list*")
    public void savePowerByRoleId(Long roleId, List<SysRes> sysResList) throws BusException {
        QueryWrapper<SysResRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleId", roleId);
        remove(queryWrapper);//先删除所有的授权信息
        List<SysResRole> list = new ArrayList<>();
        for (SysRes sysRes : sysResList) {
            list.add(new SysResRole() {{
                setRoleid(roleId);
                setResid(sysRes.getId());
            }});
        }
        saveBatch(list);
    }
}
